var jq = $.noConflict();
(function(){
	var app = angular.module('mustache', ['ionic', 'tabSlideBox', 'ionicLazyLoad', 'angular-image-404',
		'ngCordova',
		'jett.ionic.filter.bar',
		'hscrollcards',
		'ionic-modal-select',
		'oc.lazyLoad',
		'mustache.controllers',
		'mustache.services',
		'mustache.directives']);
	app.constant('configs', {
		db_name: 'app_ionic.db',
		restful_url: 'http://manage.hzxiansheng.com/app.php?s=',
		home_url: 'app.home'
	});
	app.run(function($rootScope, $ionicPlatform, $window, $cordovaDevice, $cordovaSplashscreen, Service, unit, configs) {
		var db = null;
		$ionicPlatform.ready(function() {
			if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
				cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
				cordova.plugins.Keyboard.disableScroll(true);
			}
			if (window.StatusBar) {
				// StatusBar.styleDefault();
				StatusBar.styleLightContent()
			}
			
			if(unit.isEmptyObject($window.localStorage.device)){
				var device = $cordovaDevice.getDevice();
				//var device = {uuid: null};
				$window.localStorage.setItem("device", JSON.stringify(device));
			}
			db = window.sqlitePlugin.openDatabase({name: configs.db_name, location: 'default'});
			db.transaction(function(tx) {
				tx.executeSql('CREATE TABLE IF NOT EXISTS DemoTable (name, score)');
				tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101]);
				tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202]);
			}, function(error) {
				console.log('Transaction ERROR: ' + error.message);
			}, function() {
				console.log('Populated database OK');
			});
			db.transaction(function(tx) {
				tx.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function(tx, rs) {
					console.log('Record count (expected to be 2): ' + rs.rows.item(0).mycount);
				}, function(tx, error) {
					console.log('SELECT error: ' + error.message);
				});
			});
		});
		/*xhr.sqlite({type: 'select', table: 'app_router'}).then(function(resp){
			var _menu = new Array;
			resp[0].values.forEach(function(arr){
				var _temp = {};
				for(var i in resp[0].columns){
					_temp[resp[0].columns[i]] = arr[i];
				}
				_menu.push(_temp);
			});
			console.log(_menu);
		});*/
		
	});
	app.config(function($stateProvider, $urlRouterProvider, $httpProvider, $ionicConfigProvider) {
		$httpProvider.defaults.headers = {
			post: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			get: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			put: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			delete: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}
		}
		$httpProvider.defaults.transformRequest = function(obj) {
			var str = [];
			for (var p in obj) {
				str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
			}
			return str.join("&");
		};
		$ionicConfigProvider.platform.android.tabs.style('standard');
		$ionicConfigProvider.platform.android.tabs.position('bottom');
		$ionicConfigProvider.platform.android.navBar.alignTitle('center');
		$ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
		$ionicConfigProvider.platform.android.views.transition('android');
		$stateProvider
			.state('app', {
				url: "/app",
				abstract: true,
				templateUrl: "templates/tabs-menu.html",
				controller: function($rootScope, $scope, $state, unit){
					$scope.state = ['app.home', 'app.engineers', 'app.tasks', 'app.my'];
					$rootScope.$on('$ionicView.beforeEnter', function(){
						if(unit.in_array($state.current.name, $scope.state))
							$rootScope.hideTabs = false;
						else
							$rootScope.hideTabs = true;
					});
					$scope.historyBack = function(){
						window.history.back()
					};
				}
			})
			.state('app.home', {
				url: '/home',
				views: {
					'tab-home': {
						templateUrl: 'templates/tab-home.html',
						controller: 'HomeCtrl'
					}
				}
			})
			.state("app.engineers", {
				url: "/engineers",
				cache: false,
				//abstract: true,
				views: {
					'tab-engineers': {
						templateUrl: "templates/tab-engineers.html",
						controller: 'EngineersCtrl'
					}
				}
			})
			.state('app.engineer-detail', {
				url: '/engineer-detail/:engineerId',
				views: {
					'tab-engineers': {
						templateUrl: 'templates/tab-engineers/engineer-detail.html',
						controller: 'EngineerDetailCtrl'
					}
				}
			})
			.state('app.tasks', {
				url: '/tasks',
				cache: false,
				views: {
					'tab-tasks': {
						templateUrl: 'templates/tab-tasks.html',
						controller: 'TasksCtrl'
					}
				}
			})
			.state('app.taskSearch', {
				url: '/taskSearch/:{query}',
				cache: false,
				views: {
					'tab-tasks': {
						templateUrl: 'templates/tab-tasks/tab-taskSearch.html',
						controller: 'TaskSearchCtrl'
					}
				}
			})
			.state('app.push-task',{
				url:'/push-task',
				views:{
					'tab-tasks':{
						templateUrl:'templates/tab-tasks/push-task.html',
						controller: 'PushTaskCtrl'
					}
				}
			})
			.state('app.categoryList',{
				url:'/categoryList/:cid/:name',
				views:{
					'tab-tasks':{
						templateUrl:'templates/tab-tasks/categoryList.html',
						controller: 'categoryListCtrl'
					}
				}
			})
			.state('app.task-detail',{
				url:'/task-detail/:id',
				views:{
					'tab-tasks':{
						templateUrl:'templates/tab-tasks/task-detail.html',
						controller: 'TaskDetailCtrl'
					}
				}
			})
			.state('app.center', {
				url: '/center',
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/center.html'
					}
				}
			})
			.state('app.set-headimg', {
				url: '/set-headimg',
				views:{
					'tab-my': {
						templateUrl: 'templates/tab-my/set-headimg.html'
					}
				}
			})
			.state('app.set-nickname', {
				url: '/set-nickname',
				views:{
					'tab-my': {
						templateUrl: 'templates/tab-my/set-nickname.html'
					}
				}
			})
			.state('app.bind-mobile', {
				url: '/bind-mobile',
				views:{
					'tab-my': {
						templateUrl: 'templates/tab-my/bind-mobile.html'
					}
				}
			})
			.state('app.set-password', {
				url: '/set-password',
				views:{
					'tab-my': {
						templateUrl: 'templates/tab-my/set-password.html'
					}
				}
			})
			.state('app.set-gesture-password', {
				url: '/set-gesture-password',
				views:{
					'tab-my': {
						templateUrl: 'templates/tab-my/set-gesture-password.html',
						//controller: 'nicknameCtrl'set-password
					}
				}
			})
			.state('app.my-push-task',{
				url:'/my-push-task',
				views:{
					'tab-my':{
						templateUrl:'templates/tab-my/my-push-task.html',
						controller: 'myPushTaskCtrl'
					}
				}
			})
			.state('app.my-tender-task',{
				url:'/my-tender-task',
				views:{
					'tab-my':{
						templateUrl:'templates/tab-my/my-tender-task.html',
						controller: 'myTenderTaskCtrl'
					}
				}
			})
			.state('app.helps',{
				url:'/helps',
				views:{
					'tab-my':{
						templateUrl:'templates/tab-my/helps.html',
						controller: 'helpsCtrl'
					}
				}
			})
			.state('app.question',{
				url:'/question',
				views:{
					'tab-my':{
						templateUrl:'templates/tab-my/question.html',
						controller: 'questionCtrl'
					}
				}
			})
			.state('app.favorite', {
				url: '/favorite',
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/favorite.html',
						controller: 'favoriteCtrl'
					}
				}
			})
			.state('app.my', {
				url: '/my',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my.html',
						controller: 'MyCtrl'
					}
				}
			})
			/* ------------------- === 登陆/注册 === ----------------- */
			.state('app.login', {
				url: '/login',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/login.html',
						controller: 'loginCtrl'
					}
				}
			})
			.state('app.register', {
				url: '/register',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/register.html',
						controller: 'registerCtrl'
					}
				}
			}).state('app.reg_protocol', {
				url: '/reg_protocol',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/reg_protocol.html',
						//controller: 'registerCtrl'
					}
				}
			})
			.state('app.forgot-password', {
				url: '/forgot-password',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/forgot-password.html',
					}
				}
			})
			.state('app.apply-realname', {
				url: '/apply-realname',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/apply-realname.html',
						controller: 'applyRealnameCtrl'
					}
				}
			})
			.state('app.apply-engineer',{
				url: '/apply-engineer',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/apply-engineer.html',
						controller: 'applyEngineerCtrl'
					}
				}
			})
			.state('app.account',{
				url: '/account',
				views: {
					'tab-my': {
						templateUrl: 'templates/tab-my/account.html'
						//controller: 'AccountCtrl'
					}
				}
			})
			.state('intro', {
				url: '/intro',
				templateUrl: 'templates/intro.html',
				controller: 'IntroCtrl'
			});

			if (!window.localStorage.initialRun || angular.isUndefined(window.localStorage.initialRun) || window.localStorage.initialRun === null ){
				window.localStorage.initialRun = "true";
				$urlRouterProvider.otherwise('/intro');
			}else{
				$urlRouterProvider.otherwise('/app/home');
			}
		
	});
}());